<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { useRoute } from 'vue-router'
import { SPACE_LEVEL_ENUM, SPACE_LEVEL_OPTIONS } from '@/constants/space.ts'
import {
  adminUpdateSpaceUsingPost,
  createSpaceUsingPost,
  getSpaceByIdUsingGet,
  querySpaceLevelUsingGet,
} from '@/api/spaceController.ts'
import { formatSize } from '@/utils'
import router from '@/router'

const route = useRoute()

const loading = ref(false)

const spaceForm = reactive<API.Space>({
  spaceName: '',
  spaceLevel: SPACE_LEVEL_ENUM.COMMON,
})

const spaceLevelList = ref<API.SpaceLevel[]>([])

interface Props {
  spaceType: number
}

const props = withDefaults(defineProps<Props>(), {
  spaceType: 0,
})

/**
 * 提交
 * @param any
 */
async function submit(values: any) {
  loading.value = true
  let resp
  const id = spaceForm.id
  //有id是修改，没有id是新增
  if (id) {
    resp = await adminUpdateSpaceUsingPost({
      id: id,
      ...values,
    })
  } else {
    resp = await createSpaceUsingPost({
      spaceType: props.spaceType,
      ...values,
    })
  }

  if (resp.data.code === 0 && resp.data.data) {
    message.success('执行成功')
    router.push('/')
  } else {
    message.error(resp.data.message)
  }
  loading.value = false
}

/**
 * 根据id查询空间信息
 * 当前页面作为修改页面时，才需要
 */
async function getSpaceById() {
  const id = route.query?.id
  if (id) {
    const resp = await getSpaceByIdUsingGet({ id: id })
    if (resp.data.code === 0 && resp.data.data) {
      const data = resp.data.data
      spaceForm.spaceName = data.spaceName
      spaceForm.spaceLevel = data.spaceLevel
      spaceForm.id = data.id
    } else {
      console.log('根据id查询空间信息错误')
    }
  }
}

/**
 * 获取会员信息
 */
async function querySpaceLevel() {
  const resp = await querySpaceLevelUsingGet()
  if (resp.data.code === 0 && resp.data.data) {
    spaceLevelList.value = resp.data.data
  } else {
    console.log('取会员信息列表失败' + resp.data.message)
  }
}

onMounted(() => {
  getSpaceById()
  querySpaceLevel()
})
</script>

<template>
  <div id="addSpace">
    <h2 class="title">{{ route.query?.id ? '云图库-空间修改' : '云图库-空间创建' }}</h2>

    <!--    以下是新增空间-->
    <a-form
      :model="spaceForm"
      name="RegisterForm"
      autocomplete="off"
      layout="vertical"
      @finish="submit"
    >
      <!--      名称,一定要指定name属性-->
      <a-form-item name="spaceName" label="空间名称">
        <a-input v-model:value="spaceForm.spaceName" placeholder="请输入空间名称" />
      </a-form-item>
      <!--      级别，用自动完成-->
      <a-form-item name="spaceLevel" label="空间级别">
        <a-select
          v-model:value="spaceForm.spaceLevel"
          :options="SPACE_LEVEL_OPTIONS"
          placeholder="请选择空间级别"
          allowClear
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading"
          >提交
        </a-button>
      </a-form-item>
    </a-form>

    <a-card title="空间级别介绍">
      <a-typography-paragraph> * 目前仅支持开通普通版</a-typography-paragraph>
      <a-typography-paragraph v-for="s in spaceLevelList">
        {{ s.text }} : 大小{{ formatSize(s.maxSize) }} ，数量{{ s.maxCount }}
      </a-typography-paragraph>
    </a-card>
  </div>
</template>

<style scoped>
#addSpace {
  max-width: 720px;
  margin: 0 auto;
}
</style>
